package oop.data;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import oop.db.dao.HistoryChoiceDAO;
import oop.db.dao.TestVersionDAO;
import oop.db.dao.UserDAO;


public class History {

	private long id;
	private long userId;
	private long versionId;
	private Date takenDate;
	private double mark;
	private int time;
	private Map<Long, List<Long>> choices;

	public History() {
		// default constructor
	}
	
	public History(long id, long userId, long versionId, Date takenDate, 
			double mark, int time) {
		super();
		this.id = id;
		this.userId = userId;
		this.versionId = versionId;
		this.takenDate = takenDate;
		this.mark = mark;
		this.time = time;
	}

	public User getUser() throws SQLException {
		return UserDAO.fetchById(userId).get(0);
	}
	
	public Date getTakenDate() {
		return takenDate;
	}

	public double getMark() {
		return mark;
	}

	public int getTime() {
		return time;
	}

	public long getId() {
		return id;
	}
	
	public long getVersionId() {
		return versionId;
	}
	
	public TestVersion getVersion() {
		try {
			return TestVersionDAO.fetchById(versionId);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public TestVersion getTestVersion() {
		try {
			return TestVersionDAO.fetchById(versionId);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public Test getTest() throws SQLException {
		try {
			return getTestVersion().getTest();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public Map<Long, List<Long>> getChoices() throws SQLException {
		if (choices == null) {
			choices = HistoryChoiceDAO.fetchChoices(id);
		}
		return choices;
	}
	
}
